﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComputerGraphics.Graphics;
using ComputerGraphics.Utils;

namespace ComputerGraphics.Geometry
{

    public class CompoundShape : Shape
    {
        public List<Shape> shapeList = new List<Shape>();

        public CompoundShape(Texture texture)
            : base(texture)
        {
        }

        public void AddShape(Shape shape)
        {
            if (shape.Mapping == null)
                shape.Mapping = this.Mapping;
            shapeList.Add(shape);
        }

        public override Intersection Intersect(Ray r)
        {
            double minDist = Double.MaxValue;
            Intersection ret = null;

            foreach (Shape s in shapeList)
            {
                Intersection i = s.Intersect(r);

                if (i == null) continue;

                if (i.Distance < minDist)
                {
                    minDist = i.Distance;
                    ret = i;
                }
            }

            return ret;
        }

        public override Intersection InternalIntersect(Ray r)
        {
            double minDist = Double.MaxValue;
            Intersection ret = null;

            foreach (Shape s in shapeList)
            {
                Intersection i = s.Intersect(r);
                if (i == null) continue;

                if (MathUtils.GetZeroDouble(i.Distance) > 0 && i.Distance < minDist)
                {
                    minDist = i.Distance;
                    ret = i;
                }
            }

            return ret;
        }
    }
}
